package com.zzx.utils;

import java.time.LocalDate;
import java.util.Random;

/**
 * @Author: zzx
 * @Date: 2025/7/13 14:06
 */
public class OrderIdGeneratorUtil {

    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static final int RANDOM_PART_LENGTH = 6;
    /**
     * 生成符合要求的产品订单编号
     * @return 订单编号字符串
     */
    public static String generateOrderId() {
        // 获取当前日期并格式化为YYYYMMDD
        LocalDate currentDate = LocalDate.now();
        String datePrefix = String.format("%04d%02d%02d",
                currentDate.getYear(), currentDate.getMonthValue(), currentDate.getDayOfMonth());

        // 生成6位随机大写字母+数字组合
        Random random = new Random();
        StringBuilder randomPart = new StringBuilder();
        for (int i = 0; i < RANDOM_PART_LENGTH; i++) {
            int index = random.nextInt(CHARACTERS.length());
            randomPart.append(CHARACTERS.charAt(index));
        }

        // 拼接结果
        return datePrefix + "-" + randomPart.toString();
    }

    public static void main(String[] args) {
        System.out.println(generateOrderId());
    }
}
